package com.xiehua.task.redis;

import com.xiehua.task.msg.TaskRegRespBody;
import com.xiehua.task.prop.XiehuaConfig;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisTemplate {

    private static Logger log = LoggerFactory.getLogger(RedisTemplate.class);

    private RedisTemplate() {
    }

    private static JedisPool pool;


    public static JedisPool getInstance() {
        if (pool == null) {
            synchronized (RedisTemplate.class) {
                if (pool == null) {
                    pool = initJedisPool();
                }
            }
        }
        return pool;
    }

    public static void main(String[] args) {
        testSortSet(RedisTemplate.getInstance().getResource());
    }

    public static void testSortSet(Jedis jedis) {

        try {
            System.out.println("=======集合（Set）=======");
            jedis.select(5);
            jedis.flushDB();
            Map<String, String> map = new HashMap<String, String>();
            map.put("key001", "value001");
            map.put("key002", "value002");
            map.put("key003", "value003");
            jedis.hmset("hash", map);
            jedis.hset("hash", "key004", "value004");
            // return Map<String,String>
            System.out.println("散列hash的所有键值对为：" + jedis.hgetAll("hash111111111111111111111"));
            // return Set<String>
            System.out.println("散列hash的所有键为：" + jedis.hkeys("hash"));
            // return List<String>
            System.out.println("散列hash的所有值为：" + jedis.hvals("hash"));
            System.out.println("将key006保存的值加上一个整数，如果key006不存在则添加key006：" + jedis.hincrBy("hash", "key006", 6));
            System.out.println("散列hash的所有键值对为：" + jedis.hgetAll("hash"));
            System.out.println("将key006保存的值加上一个整数，如果key006不存在则添加key006：" + jedis.hincrBy("hash", "key006", 3));
            System.out.println("散列hash的所有键值对为：" + jedis.hgetAll("hash"));
            System.out.println("删除一个或者多个键值对：" + jedis.hdel("hash", "key002"));
            System.out.println("散列hash的所有键值对为：" + jedis.hgetAll("hash"));
            System.out.println("散列hash中键值对的个数：" + jedis.hlen("hash"));
            System.out.println("判断hash中是否存在key002：" + jedis.hexists("hash", "key002"));
            System.out.println("判断hash中是否存在key003：" + jedis.hexists("hash", "key003"));
            System.out.println("获取hash中的值：" + jedis.hmget("hash", "key003"));
            System.out.println("获取hash中的值：" + jedis.hmget("hash", "key003", "key004"));

            System.out.println("");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    public static Jedis getJedis(){
        return getInstance().getResource();
    }

    /**
     * @Description: 释放jedis到jedisPool中
     * @Param: [jedis, ip, port]
     * @return: void
     * @Author: imdalai
     * @Date: 2018/1/15
     */
    public static void closeRedis(Jedis jedis) {
        if (jedis != null) {
            try {
                jedis.close();
            } catch (Exception e) {
                log.error("colse jedis failed ! " + e.getMessage(), e);
            }
        }
    }

    private static JedisPool initJedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        //配置最大jedis实例数
        config.setMaxTotal(1000);
        //配置资源池最大闲置数
        config.setMaxIdle(200);
        //等待可用连接的最大时间
        config.setMaxWaitMillis(10000);
        config.setTestOnBorrow(true);
        Map<String,String> configMap = XiehuaConfig.getInstance();
        return  new JedisPool(
                config,
                configMap.get(XiehuaConfig.REDIS_HOST),
                Integer.parseInt(configMap.get(XiehuaConfig.REDIS_PORT)),
                10000,
                configMap.get(XiehuaConfig.REDIS_PWD),
                Integer.parseInt(configMap.get(XiehuaConfig.REDIS_DB)));
    }




}
